<?php
/**
 * Berechnung des Abstands zweier Koordinaten
 *
 * @author		Joachim Ruf
 * @copyright	Copyright (c) 2003-2013 Loresoft Software (http://www.loresoft.de)
 * @license		http://yapary.loresoft.de/lizenzbedingungen     New BSD License
 *
 * */


Class Yapary_Math_Coordinate
{


	function __construct ( )
	{

	}


	/**
	 * @desc Berechnet anhand zweier Koordinaten, den Luftabstand in Km mit der Praezision $_precision = 2 Nachkommastellen
	 * */
	public function getDistance ($_lat1, $_lng1, $_lat2, $_lng2, $_precision = 2) {
		//return round($this->getDistanceByPythagoras($_lat1, $_lng1, $_lat2, $_lng2), $_precision);
		return number_format($this->getDistanceBySurfaceOfSphere($_lat1, $_lng1, $_lat2, $_lng2), $_precision);
	}


	// @desc Berechnung durch Kugeloberflaeche (Exakte methode)
	private function getDistanceBySurfaceOfSphere ($_lat1, $_lng1, $_lat2, $_lng2) {
		$rad	= 0.01745; // 3.14159265 : 180 Grad
		return (6378.388 * $rad) * (acos(sin($_lat1)*sin($_lat2) + cos($_lat1)*cos($_lat2) * cos($_lng2 - $_lng1)));
	}


	// @desc Berechnung durch Pythagoras (Annaeherung)
	private function getDistanceByPythagoras ($_lat1, $_lng1, $_lat2, $_lng2) {
		$dx = 71.5 * ($_lng1 - $_lng2);
		$dy = 111.3 * ($_lat1 - $_lat2);

		return sqrt($dx*$dx + $dy*$dy);
	}


}


?>